package com.medical_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical_backend.entity.Payment;
import com.medical_backend.entity.Policy;
import com.medical_backend.entity.Reimbursement;
import com.medical_backend.entity.Result;
import com.medical_backend.service.PolicyService;
import com.medical_backend.service.ReimbursementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reimbursement")
public class ReimbursementController {
    @Autowired
    private ReimbursementService reimbursementService;

    @Autowired
    private PolicyService policyService;

    //获取慢病信息列表
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {

        QueryWrapper<Reimbursement> queryWrapper = new QueryWrapper<>();

        // 关键词搜索：身份证或姓名模糊匹配
        if (StringUtils.hasText(keyword)) {
            queryWrapper.lambda()
                    .like(Reimbursement::getCardId, keyword)
                    .or()
                    .like(Reimbursement::getInsuredName, keyword);
        }

        // 审核状态过滤
        if (StringUtils.hasText(status)) {
            if ("未审核".equals(status)) {
                queryWrapper.lambda().eq(Reimbursement::getIsReimbursement, "未审核");
            } else if ("已审核".equals(status)) {
                queryWrapper.lambda().in(Reimbursement::getIsReimbursement, "通过", "拒绝");
            } else if ("未汇款".equals(status)) {
                queryWrapper.lambda().eq(Reimbursement::getIsRemit, "未汇款");
            } else if ("已汇款".equals(status)) {
                queryWrapper.lambda().eq(Reimbursement::getIsRemit, "已汇款");
            }
            // 其它自定义状态你可以继续往下加
        }

        // 分页查询
        Page<Reimbursement> pageInfo = reimbursementService.page(new Page<>(page, size), queryWrapper);
        return new Result("查询成功", pageInfo, 200);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Reimbursement reimbursement){
        int year = reimbursement.getDate().getYear();
        System.out.println("今年是："+year);
        List<Policy> list = policyService.list();
        //判断当年是否存在政策
        for (Policy policy:list){
            if(policy.getYear().equals(year+"")){
                //判断报销金额是否超过
                if(reimbursement.getTotalCast()>policy.getMax()){
                    return new Result("添加失败", null, 200);
                }else{
                    reimbursement.setReimbursementAmount(reimbursement.getTotalCast()*policy.getPercentage());
                    reimbursement.setIsReimbursement("未审核");
                    reimbursement.setIsRemit("未汇款");
                    return new Result("添加成功",reimbursementService.save(reimbursement),200);
                }
            }
        }
        return new Result("添加失败",null,500);
    }

    @PostMapping("/edit")
    public Result update(@RequestBody Reimbursement reimbursement){
        reimbursementService.updateById(reimbursement);
        return new Result("修改成功",null,200);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean isDeleted = reimbursementService.removeById(id);
        if (isDeleted) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败: 慢病政策不存在", null, 404);
        }
    }

    @GetMapping("/getaudit")
    public Result getAudit(){
        QueryWrapper<Reimbursement> queryWrapper = new QueryWrapper<Reimbursement>().eq("is_reimbursement","未审核");
        return new Result("查询成功",reimbursementService.list(queryWrapper),200);
    }

    @GetMapping("/getremit")
    public Result getRemit(){
        QueryWrapper<Reimbursement> queryWrapper = new QueryWrapper<Reimbursement>().eq("is_reimbursement","已审核")
                .eq("is_remit","未汇款");
        return new Result("查询成功",reimbursementService.list(queryWrapper),200);
    }

    @GetMapping("/getstatics")
    public Result getstatics(){
        QueryWrapper<Reimbursement> reimbursementQueryWrapper = new QueryWrapper<Reimbursement>().eq("is_reimbursement","通过");
        List<Reimbursement> allowReimbursements = reimbursementService.list(reimbursementQueryWrapper);
        QueryWrapper<Reimbursement> reimbursementQueryWrapper1 = new QueryWrapper<Reimbursement>().eq("is_reimbursement","拒绝");
        List<Reimbursement> rejectReimbursements = reimbursementService.list(reimbursementQueryWrapper1);
        QueryWrapper<Reimbursement> reimbursementQueryWrapper2 = new QueryWrapper<Reimbursement>().eq("is_reimbursement","未审核");
        List<Reimbursement> reimbursements = reimbursementService.list(reimbursementQueryWrapper2);
        Map<String,Integer> map = new HashMap();
        map.put("未审核", reimbursements.size());
        map.put("拒绝", rejectReimbursements.size());
        map.put("通过", allowReimbursements.size());
        return new Result("查询成功", map, 200);
    }

    @GetMapping("/getremitstatics")
    public Result getRemitStatics() {
        QueryWrapper<Reimbursement> remitYesWrapper = new QueryWrapper<Reimbursement>()
                .eq("is_remit", "已汇款");
        QueryWrapper<Reimbursement> remitNoWrapper = new QueryWrapper<Reimbursement>()
                .eq("is_remit", "未汇款");

        int remitYesCount = (int) reimbursementService.count(remitYesWrapper);
        int remitNoCount = (int) reimbursementService.count(remitNoWrapper);

        Map<String, Integer> map = new HashMap<>();
        map.put("已汇款", remitYesCount);
        map.put("未汇款", remitNoCount);

        return new Result("查询成功", map, 200);
    }
}
